
// A1_compute_import_prices
//==============================================================================

// Description: This file computes average import prices for each EU country that are
// then used in Figure 1


clear
clear matrix
set more off

cd "D:\data_replication"

// Import and format trade and income data
//------------------------------------------------------------------------------

use data\trade\EU_imports_cn8_plus_summary_USD.dta, clear

keep if year==2007
drop if price_ton_USD==.

drop if partner < 100              							// Keep EU Countries


// Construct average prices
//------------------------------------------------------------------------------

sort year cn8plus declarant
by year cn8plus declarant: egen mean_price = mean(price_ton_USD)
by year cn8plus declarant: egen total_imports = sum(imports_year_USD)
gen share_imports = imports_year_USD/total_imports
gen price_weighted = share_imports*price_ton_USD
by year cn8plus declarant: egen price_avg_weighted = sum(price_weighted)
sort year cn8plus declarant
by year cn8plus declarant: keep if _n==1
gen mean_price_log = log(mean_price)
gen price_avg_weighted_log = log(price_avg_weighted)

merge m:1 declarant year using data/gdp_per_capita/GDP_per_capita_declarant.dta
keep if _merge==3
drop _merge
gen gdp_capita_declarant_log = log(gdp_per_capita_defl_declarant)


// Create importer dummies
//------------------------------------------------------------------------------

forval i=1/100 {
gen s`i' = 0
replace s`i' = 1 if declarant == `i'
egen sum_s = sum(s`i')
if sum_s[1]==0 {
drop s`i'
}
drop sum_s
}


// Run regression and save output
//------------------------------------------------------------------------------

xtset cn8plus
xtreg price_avg_weighted_log s3-s91, fe vce(cluster cn8plus)
estout using statistics_and_graphs\import_prices\output_files\prices_importer.txt, replace
